package com.miguo.activity.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.miguo.activity.LoginActivity;
import com.miguo.activity.base.BaseFragment;
import com.miguo.framework.request.JsonData;
import com.miguo.framework.utils.Toast;
import com.miguo.framework.view.CustomAutoCompleteTextView;
import com.miguo.http.HttpClient;
import com.miguo.http.Url;
import com.miguo.http.callback.AjaxCallBack;
import com.miguo.ui.R;
import com.miguo.utils.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * File Names: ForgetFragment
 * Describe:
 * User: LinXiaoZhi(87100854@qq.com)
 * Date: 2015-08-12
 * Time: 23:25
 */
public class ForgetFragment extends BaseFragment implements View.OnClickListener {

    private View contentView;

    private String mUserName;

    private LoginActivity mLoginActivity;

    private CustomAutoCompleteTextView mNameEdt;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.fragment_forget, container, false);
        return contentView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mLoginActivity = (LoginActivity) this.getActivity();
        initTitle();
        initView();
        initListener();
    }

    private void initTitle() {
        mLoginActivity.mBaseHead.getRightTextView().setVisibility(View.VISIBLE);
        mLoginActivity.mBaseHead.setTitle(R.string.login_forget);
        mLoginActivity.mBaseHead.getRightTextView().setText("登录");
        mLoginActivity.mBaseHead.getRightTextView().setOnClickListener(this);
    }

    private void initView() {
        mNameEdt = (CustomAutoCompleteTextView) contentView.findViewById(R.id.forget_name_edt);
    }

    private void initListener() {
        contentView.findViewById(R.id.forget_btn).setOnClickListener(this);
    }

    private void forget() {
        mUserName = this.mNameEdt.getText().toString();
        if (StringUtils.isEmail(mUserName)) {
            sendMail();
        } else {
            mNameEdt.setShakeAnimation();
            Toast.show(mContext, "请输入正确的邮箱");
            return;
        }
    }


    private void sendMail() {
        AjaxCallBack callBack = new AjaxCallBack(mActivity, "正在发送,请稍候...") {
            @Override
            public void onRequestFinish(JsonData json) {
                super.onRequestFinish(json);
                Toast.show(mContext, json.optString("info"));
            }
        };
        Map<String, String> param = new HashMap<String, String>();
        param.put("email", mUserName);
        HttpClient.post(Url.Passport.Forget.SendMail, param, callBack);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.right_tv:
                mLoginActivity.replaceFragment(LoginFragment.class);
                break;
            case R.id.forget_btn:
                forget();
                break;
        }
    }
}
